var basePath;
$(function(){
	sendCode();
	protocol();
	basePath = $(".basePath").attr("url");
	if(basePath == undefined){
		basePath = $("#contextPath").val();
	}
});
var phone_reg = /^1[3|4|5|7|8]\d{9}$/;
var pa=/^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,20}$/;
//用户登录
var userLogin= function() {
		var userName = $("#userName").val();
		var password = $("#password").val();
		var forward = $("#forward").val();
		var terminal = $("#terminal").val();
		 if($.trim(userName).length<=0){
			 layer.open({
				    content:'请输入手机号码',
				    time: 2
				});
			 return false;
		 }
		 if(!phone_reg.test(userName)){ 
			 layer.open({
				    content:'您输入的手机号码不合法,请重新输入',
				    time: 2
				});
	        return false; 
	     } 
		 if($.trim(password).length<=0){
			 layer.open({
				    content:'请输入您的密码!',
				    time: 2
				});
			 return false; 
		 }
		 
		 
		 if(forward != ''){	 
			 forward = basePath+forward;
		 }else{
			 forward = basePath+"/";
		 }
		 
		 var param = {userName:userName,pwd:password};
		 $.ajax({
			 url:'login',
			 async:true,
			 data:param,
			 type:'post',
			 dataType:"json",
			 success:function(data, textStatus){
				 if(data.result.state ==0 && data.result.message == 'user not exists'){
					 layer.open({
						    content:'此账号不存在!',
						    time: 2
					});
				 }
				 if(data.result.code == 'err'){
					 layer.open({
						 content:'密码错误!',
						 time: 2
					 });
				 }
				 if(data.result.code == 'error'){
					 layer.open({
						 content:'用户不存在!',
						 time: 2
					 });
				 }
				 if(data.result.state == 1){
					 if(data.result.message == 'userIsNull'){
						 layer.open({
							    content:'手机号或密码不正确',
							    time: 2
							});
					 }else{
						 var next = data.next;
						 var userId = data.user.id;
						 var spm = (terminal == 'app')?('?spm='+userId):'';

						 /*layer.open({
							    content:'登录成功!',
							    time: 2
							});*/
						 var cookie = document.cookie;
						 if(cookie.length > 0){
							 $.ajax({
								 url:basePath+'/common/synCookieResult',
								 async:true,
								 type:'post',
								 dataType:"json",
								 success:function(data, textStatus){
										 var exp = new Date();
										 exp.setTime(exp.getTime() - 1);
										 document.cookie = "HLTX_SHOPCART=0;expires="+exp.toGMTString()+";path=/";

										 window.location = forward + spm;
										 
								 }
							 });  
						 }else{
							 window.location = forward + spm;
						 } 
					 }
				 }
			 },
			 error:function(XMLHttpRequest, textStatus, errorThrown){
				 layer.open({
					    content:textStatus,
					    time: 2
					});
			 }
		 });
}
	/**发送验证码**/
var sendCode = function(){
	$(".sendCode").on("click",function(){
	  var ele = $(".ui_btn_success")
	  if(!ele.attr('value')){
	          ele.attr('value',1);
						var userName = $("#userName").val();
						var type = $("#types").val();
						if($.trim(userName).length<=0){
							layer.open({
							    content:'请输入注册时使用的手机号码!',
							    time: 2
							});
							ele.removeAttr('value');
							return false; 
						}
						if(!phone_reg.test(userName)){
							 layer.open({
								    content:'你所输入手机号码不合法请仔细检查!',
								    time: 2
								});
							 ele.removeAttr('value');
							 return false; 
						} 
						$.ajax({
							url:basePath+'/send',
							data:{userName:userName,type:type},
							type:'post',
							success:function(data){
								if(data.result.state == 0 && data.result.code == '100005'){
									layer.open({
									    content:'此用户已注册!',
									    time: 2
									});
									ele.removeAttr('value');
								}
								if(data.result.state == 0 && data.result.code == 'error'){
									layer.open({
									    content:'此用户不存在!',
									    time: 2
									});
									 ele.removeAttr('value');
								}
								if(data.result.state == 1){
									$("#codeHidden").val(data.result.code);
									countdown(data.result.message);
								}
								if(data.result.state == 0 &&  data.result.code == "0000"){
									layer.open({
									    content:data.result.message,
									    time: 2
									});
									 ele.removeAttr('value');
								}
							},
							error:function(e){
								 layer.open({
									    content:'获取验证码错误请稍刷新后再试',
									    time: 2
								   });
								 ele.removeAttr('value');
							}
						});
	      }
		 });
	 
}
var seTips = function() {
	$(".tips-box").css({'color':'red'});
	setTimeout(function(){
		$(".tips-box").html('');
	}, 2000)
}



var countdown = function (msg){
	var ele = $(".ui_btn_success")
	var miao = 120;
	var time;
	(function timedown(msg){
		miao-=1;
		if(miao<0){
			clearTimeout(time)
			ele.css({backgroundColor:'#00b18f',color:'#fff'});
			ele.removeAttr('value');
			ele.html('获取验证码');
		}else{
			ele.html(miao+'秒后重发');
			ele.css({backgroundColor:'#eee',color:'#666'});
			setTimeout(timedown,1000)
		}
	}(msg));
}



/**注册**/

var registerBtn = function () {
	var userName = $("#userName").val();
	var pasword = $("#pasword").val();
	var code = $("#code").val();
	if($.trim(userName).length<=0){
		layer.open({
		    content:'请输入注册时使用的手机号码!',
		    time: 2
		});
		 return false; 
	}
	if(!phone_reg.test(userName)){
		layer.open({
		    content:'你所输入手机号码不合法请仔细检查!',
		    time: 2
		});
		 return false; 
	}
	
	if($.trim(pasword).length <= 0) {
		layer.open({
		    content:'你所输入密码',
		    time: 2
		});
		return false;
	}
	if(!pa.test(pasword)){
		layer.open({
		    content:'密码有6~20位英文字母/数字或符号组成',
		    time: 2
		});
		return false;
	}
	if($.trim(code).length <=0 ){
		layer.open({
		    content:'你所输入验证码',
		    time: 2
		});
		return false;
	}
	var isProtocol = $(".simulation_checkbox_active");
	if(isProtocol.length <= 0 ){
		layer.open({
		    content:'请同意协议后在注册',
		    time: 2
		});
		return false;
	}
	var pamam = {mobile:userName,pasword:pasword,code:code,type:'register'}
	$.ajax({
		url:'register',
		data:pamam,
		type:'post',
		success:function(data){
			if(data.result.code == 'exist'){
				layer.open({
				    content:'此手机号码已注册请更换其他号码!',
				    time: 2
				});
				return false;
			}
			if(data.result.code == 'error'){
				layer.open({
				    content:'验证码输入错误!',
				    time: 2
				});
				return false;
			}
			if(data.result.code == 'expire'){
				layer.open({
				    content:'验证码已过期!',
				    time: 2
				});
				return false;
			}
			if(data.result.code == 'ok'){
//				layer.open({
//				    content:'恭喜您注册成功',
//				    time: 2
//				});
				$.ajax({
					 url:basePath+'/common/synCookieResult',
					 async:true,
					 type:'post',
					 dataType:"json",
					 success:function(data, textStatus){
						 	debugger;
							 var exp = new Date();
							 exp.setTime(exp.getTime() - 1);
							 document.cookie = "HLTX_SHOPCART=0;expires="+exp.toGMTString()+";path=/";
							 window.location=basePath;
					 }
				 });
				/*window.location=basePath+"/friend/toRS";*/
				return false;
			}
		},
		error:function(data){
			
		}
	});
}

/**验证手机收到验证码是否有效**/
var UpdateNewPwd = function(){
	var userPhone = $("#userName").val();
	var code = $("#code").val();
	var password = $("#types").val();
	if(phone_reg.test(userName)){
		layer.open({
		    content:'你所输入手机号码不合法请仔细检查!',
		    time: 10000
		});
		return false; 
	}
	if(code.length <=0){
		layer.open({
		    content:'请输入手机收到的验证码!',
		    time: 10000
		});
		return false; 
	}
	var params = {mobile:userPhone,code:code,type:password};
	$.ajax({
		url:'toNewsPwd',
		data:params,
		type:'post',
		dataType:'json',
		success:function(data){
				if(data.result.state ==1 && data.result.code =='expire'){
					layer.open({
					    content:'验证码失效!',
					    time: 10000
					});
					return false;
				}
				if(data.result.message != undefined && data.result.message == '0001'){
					layer.open({
					    content:'验证码输入错误!',
					    time: 10000
					});
					return false;
				}
				countdown(data.result.message);
				if (data.next != '') {
					window.location =basePath+ data.next;
				}
		},
		error:function(data){
			layer.open({
			    content:data,
			    time: 2
			});
		}
	});
}

var updateNewsPwdIng = function (){
	var userName = $("#userName").val();
	var reg_password = $("#reg_password").val();
	var password = $("#password").val();
	if($.trim(password).lenght <= 0){
		layer.open({
		    content:'请输入新密码',
		    time: 10000
		});
		return false;
	}
	if(!pa.test(password)){
		layer.open({
		    content:'密码不合法请重新设置',
		    time: 10000
		});
		return false;
	}
	
	if($.trim(reg_password).lenght <= 0){
		layer.open({
		    content:'请输入确认密码',
		    time: 10000
		});
		return false;
	}
	if($.trim(reg_password) != $.trim(password)){
		layer.open({
		    content:'你输入密码不一致',
		    time: 10000
		});
		return false;
	}
	var param = {userName:userName,password:password};
	$.ajax({
		url:'updateNewPwd',
		data:param,
		type :'post',
		dataType:'json',
		success:function(data){
			if(data.result.state == 0 ){
				layer.open({
				    content:data.result.message,
				    time: 10000
				});
			}
			window.location =basePath+ data.next;
		},
	});
} 

var protocol = function (){
	form.Checkbox('.reg_notice', {
	    onchange: function(Checkbox, _this) {
	      var is = Checkbox.iSelect,
	        ele = Checkbox.ele;
	      if (_this.id === 'iSelect') {
	        if (!is) {
	          [].forEach.call(ele, function(ele) {
	            ele.querySelector('.simulation_checkbox').classList.remove('simulation_checkbox_active');
	          });
	        } else {
	          [].forEach.call(ele, function(ele) {
	            ele.querySelector('.simulation_checkbox').classList.add('simulation_checkbox_active');
	          });
	        }

	      }
	    }
});	
}

var NumberToTime =  function (time){
	var min = parseInt(time/60);
	var sec = time - (min*60);
	if(min == '0'){
		return sec+"秒";
	}
	if(sec == '0' ){
		return min+"分钟"
	}else{
		
		return min+"分钟"+sec+"秒";
	}
}













